package monster.flight.admin.manager.imp.xtgl;

import java.util.ArrayList;
import java.util.List;

import monster.comm.key.Key;
import monster.comm.util.Tool;
import monster.flight.admin.manager.service.xtgl.MenuService;
import monster.flight.comm.entity.dao.Xt_menDAO;
import monster.flight.comm.entity.vo.Xt_men;
import monster.flight.comm.util.KeyConstant;

/**
 * 创建信息=============
 * 类名: monster.flight.admin.manager.imp.xtgl.MenuServiceImp
 * 类作用描述: 菜单管理实现类
 * 创建人： wanghong 
 * 创建时间： 2013-1-15 下午04:59:19
 * 修改信息1=============
 * 修改类型:     //1.修改bug 2.业务变化修改
 * 修改描述: 
 * 修改人：  
 * 修改时间：
 */

public class MenuServiceImp implements MenuService {
	private Xt_menDAO xt_menDAO;
	

	public Xt_menDAO getXt_menDAO() {
		return xt_menDAO;
	}

	public void setXt_menDAO(Xt_menDAO xt_menDAO) {
		this.xt_menDAO = xt_menDAO;
	}

	@Override
	public List<Xt_men> getAllMenu() throws Exception {
		List<Xt_men> list = xt_menDAO.getAllMenu();
		if(list==null){
			list = new ArrayList<Xt_men>(0);
		}
		return list;
	}
	@Override
	public void insert(Xt_men xt_men) throws Exception {
		Tool.NullException(xt_men, "xt_men 不能为空");
		Tool.NullException(xt_men.getParent_sn(), "parent_sn 不能为空");
		Tool.NullException(xt_men.getLevel_index(), "level_index 不能为空");
		int key = Key.getKey(KeyConstant.XT_KEY_SN);
		xt_men.setMen_sn(String.valueOf(key));
		if ("0".equals(xt_men.getParent_sn())) {// 第一层
			xt_men.setFomcat("." + xt_men.getMen_sn() + ".");//不用传入Fomcat由men_sn生成
		} else {
			Tool.NullException(xt_men.getFomcat(), "fomcat 不能为空");//如果不是第一层要传入父fomcat
			String fomat = xt_men.getFomcat() + xt_men.getMen_sn() + ".";
			xt_men.setFomcat(fomat);
			int index = Integer.parseInt(xt_men.getLevel_index()) + 1;// 层数
			xt_men.setLevel_index(String.valueOf(index));
		}
		xt_menDAO.insert(xt_men);
	}


	@Override
	public void update(Xt_men xt_men) throws Exception {
		Tool.NullException(xt_men, "xt_men 不能为空");
		Tool.NullException(xt_men.getMen_sn(), "men_sn 不能为空");
		xt_menDAO.update(xt_men);
	}	
	@Override
	public void deleteMenAndChild(String fomcat) throws Exception {
		Tool.NullException(fomcat, "fomcat 不能为空");		
		xt_menDAO.deleteMenAndChild(fomcat);
	}


	@Override
	public boolean isHasChildren(String id) throws Exception {
		Tool.NullException(id, "id 不能为空");		
		return xt_menDAO.isHasChildren(id)>0;
	}

	@Override
	public Xt_men getPoByID(String id) throws Exception {
		Tool.NullException(id, "id 不能为空");		
		return xt_menDAO.getPoByID(id);
	}

	@Override
	public boolean vild_MenuCode(String men_code, String old_men_code)
			throws Exception {
		Tool.NullException(men_code, "menu_code不能为空");
		boolean ret = true;
		if(!men_code.equals(old_men_code)){
			ret=xt_menDAO.vild_MenuCode(men_code)>0?false:true;
		}
		
		return ret;
	}

}
